package com.yt.xiao.code.generate.util;

public final class CgUtil {

    public static void main(String[] arg) throws Exception{
        String s1 = "3_is_-use-abc";
        String s2 = CgUtil.toCamelCase(s1);
        String s3 = CgUtil.camelToUnderLineCase(s2);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
    }

    /**
     * 根据驼峰规则由字段名称生成变量名称<br />
     * 分隔符可以是下划线('_')或者 短横线('-'),其他分隔符将被忽略<br />
     * 方法将忽略 非字母(大小写)数字字符, 如果数字字符在首位将被忽略<br />
     * @param code 待处理的字段名称
     * @return 处理后的值
     */
    public static final String toCamelCase(String code){
        if(code==null||code.trim().length()<1){
            return null;
        }else{
            code = code.trim().toLowerCase();
            StringBuilder sb = new StringBuilder();
            boolean nwuf=false;//下一个字母大写标志
            for(int i=0;i<code.length();i++){
                char c = code.charAt(i);
                if(c=='_' || c=='-'){
                    if(sb.length()<1){
                        continue;
                    }else{
                        nwuf = true;
                    }
                }else{//如果当前字符是 [小写字母,数字] , 上面已经将全部字符串转成小写,此处不存在大写字母(c>64&&c<91)
                    if( (c>96 && c<123) || (c>47 && c<58) ){
                        if(c>96 && c<123){//如果当前是消协字母
                            if(nwuf==true){
                                sb.append((char)(c-32));
                                nwuf=false;
                            }else{
                                sb.append(c);
                            }
                        }else{//如果是数字
                            if(sb.length()>0){
                                sb.append(c);
                            }
                        }
                    }
                }
            }
            
            return sb.length()<1 ? null : sb.toString();
        }
    }

    /**
     * 由驼峰名称规则变量转换为下划线名称方式变量
     * @param code 待处理的驼峰名称变量
     * @return 下划线名称规则变量
     */
    public static final String camelToUnderLineCase(String code){
        if(code==null||code.trim().length()<1){
            return null;
        }else{
            StringBuilder sb = new StringBuilder();
            for(int i=0;i<code.length();i++){
                char c = code.charAt(i);
                if(c>64&&c<91){//大写字母
                    sb.append('_').append((char)(c+32));//转成小写字母
                }else if(c>47 && c<58){//数字
                    sb.append('_').append((char)(c));
                }else if( (c>96 && c<123) ){
                    sb.append(c);
                }
            }
            return sb.length()<1 ? null : sb.toString();
        }
    }

    /**
     * 根据sqlType 获取java 类型
     * @param sqlType
     * @return
     */
    public static final String getJavaType(int sqlType){
        String javaType;
        switch(sqlType){
        case java.sql.Types.BIT:
            javaType = "Byte";
            break;
        case java.sql.Types.TINYINT:
            javaType = "Byte";
            break;
        case java.sql.Types.SMALLINT:
            javaType = "Short";
            break;
        case java.sql.Types.INTEGER:
            javaType = "Integer";
            break;
        case java.sql.Types.BIGINT:
            javaType = "Long";
            break;
        case java.sql.Types.FLOAT:
            javaType = "Float";
            break;
        case java.sql.Types.REAL:
        case java.sql.Types.DOUBLE:
            javaType = "Double";
            break;
        case java.sql.Types.NUMERIC:
        case java.sql.Types.DECIMAL:
            javaType = "java.math.BigDecimal";
            break;
        case java.sql.Types.CHAR:
            /*if(length==0){
                javaType = "Character";
            }else{
                javaType = "String";
            }
            break;*/
        case java.sql.Types.VARCHAR:
        case java.sql.Types.LONGVARCHAR:
        case java.sql.Types.NCHAR:
        case java.sql.Types.NVARCHAR:
        case java.sql.Types.LONGNVARCHAR:
        case java.sql.Types.CLOB:
        case java.sql.Types.NCLOB:
            javaType = "String";
            break;
        case java.sql.Types.DATE:
        case java.sql.Types.TIME:
            javaType = "java.util.Date";
            break;
        case java.sql.Types.TIMESTAMP:
            //javaType = "Timestamp";
            javaType = "java.util.Date";
            break;
        case java.sql.Types.BINARY:
        case java.sql.Types.VARBINARY:
        case java.sql.Types.LONGVARBINARY:
        case java.sql.Types.BLOB:
            javaType = "byte[]";
            break;
        case java.sql.Types.BOOLEAN:
            javaType = "Boolean";
            break;
        /*case java.sql.Types.XXXXX:
            javaType = "XXXXX";
            break;*/
        default:
            javaType=null;
            break;
        }
        return javaType;
    }

    /**
     * 首字母转大写
     * @param str
     * @return
     */
    public static final String capFirst(String str){
        if(str!=null && str.trim().length()>0){
            char fc = str.charAt(0);
            if(fc>96 && fc<123){
                str = ((char)(fc-32)) +str.substring(1);
            }
        }
        return str;
    }

    /**
     * 根据java 类型获取转换方法名称
     * @param javaType
     * @return
     */
    public static final String getTranslateMethodName(String javaType){
        String method;
        if(javaType!=null && javaType.trim().length()>0){
            method = null;
        }else{
            if(javaType.endsWith("[]")){
                if(javaType.equals("byte[]")){
                    method = "transformByteArray";
                }else{
                    method=null;
                }
            }else{
                method = "transform"+capFirst(javaType);
            }
        }
        return method;
    }

    /**
     * 解析javaSqlType
     * */
    public static String getSqlTypeName(int sqlType){
        String javaSqlTypeName;
        switch(sqlType){
        case java.sql.Types.BIT:
            //javaType = "Boolean";
            javaSqlTypeName="java.sql.Types.BIT";
            break;
        case java.sql.Types.TINYINT:
//            if(size==1){
//                javaType = "Boolean";
//                javaSqlTypeName="java.sql.Types.TINYINT";
//            }else{
//                javaType = "Byte";
//                javaSqlTypeName="java.sql.Types.TINYINT";
//            }
            //javaType = "Byte";
            javaSqlTypeName="java.sql.Types.TINYINT";
            break;
        case java.sql.Types.SMALLINT:
            //javaType = "Short";
            javaSqlTypeName="java.sql.Types.SMALLINT";
            break;
        case java.sql.Types.INTEGER:
            //javaType = "Integer";
            javaSqlTypeName="java.sql.Types.INTEGER";
            break;
        case java.sql.Types.BIGINT:
            //javaType = "Long";
            javaSqlTypeName="java.sql.Types.BIGINT";
            break;
        case java.sql.Types.FLOAT:
            //javaType = "Double";
            javaSqlTypeName="java.sql.Types.FLOAT";
            break;
        case java.sql.Types.REAL:
            //javaType = "Float";
            javaSqlTypeName="java.sql.Types.REAL";
            break;
        case java.sql.Types.DOUBLE:
            //javaType = "Double";
            javaSqlTypeName="java.sql.Types.DOUBLE";
            break;
        case java.sql.Types.NUMERIC:
            //javaType = "BigDecimal";
            javaSqlTypeName="java.sql.Types.NUMERIC";
            break;
        case java.sql.Types.DECIMAL:
            //javaType = "BigDecimal";
            javaSqlTypeName="java.sql.Types.DECIMAL";
            break;
        case java.sql.Types.CHAR:
            //javaType = "String";
            javaSqlTypeName="java.sql.Types.CHAR";
            break;
        case java.sql.Types.VARCHAR:
            //javaType = "String";
            javaSqlTypeName="java.sql.Types.VARCHAR";
            break;
        case java.sql.Types.LONGVARCHAR:
            //javaType = "String";
            javaSqlTypeName="java.sql.Types.LONGVARCHAR";
            break;
        case java.sql.Types.DATE:
            //javaType = "Date";
            javaSqlTypeName="java.sql.Types.DATE";
            break;
        case java.sql.Types.TIME:
            //javaType = "Date";
            javaSqlTypeName="java.sql.Types.TIME";
            break;
        case java.sql.Types.TIMESTAMP:
            //javaType = "Date";
            javaSqlTypeName="java.sql.Types.TIMESTAMP";
            break;
        case java.sql.Types.BINARY:
            //javaType = "byte[]";
            javaSqlTypeName="java.sql.Types.BINARY";
            break;
        case java.sql.Types.VARBINARY:
            //javaType = "byte[]";
            javaSqlTypeName="java.sql.Types.VARBINARY";
            break;
        case java.sql.Types.LONGVARBINARY:
            //javaType = "byte[]";
            javaSqlTypeName="java.sql.Types.LONGVARBINARY";
            break;
        //case java.sql.Types.NULL:
            //break;
        default:
            //javaType = "String";
            javaSqlTypeName="java.sql.Types.UNKNOWN";
            break;
        }
        return javaSqlTypeName;
    }
}
